home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Interapplication Communication / AECDEV⁄AEDAEMON / AEDaemonAEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-09  |  3.7 KB  |  108 lines  |  [TEXT/MPS ]

  1. /* Here are the AppleEvent handlers for this background-only task. */
  2. /* Since this is a real live application, it can accept and send any */
  3. /* Apple Events you want to send or recieve.  */
  4. /* In this case, the only ones that make any sense in this app are */
  5. /* 'oapp' and 'quit' */
  6.  
  7. #include "AEDaemon.h"
  8. extern Boolean gQuit;
  9. extern EventRecord ERecord;
  10. extern Boolean gHasAppleEvents;
  11.  
  12. /* InitAEStuff checks for the availability of the AppleEvent Manager and */
  13. /* installs our event handlers. */
  14. /* if the AEM isn't around, we bail. */
  15. void InitAEStuff(void)
  16. {
  17.  
  18.     OSErr aevtErr = noErr;
  19.     long aLong = 0;
  20.     Boolean gHasAppleEvents = false;
  21.     /* Check this machine for AppleEvents.  If they are not here (ie not 7.0)
  22.     *   then we exit */
  23.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  24.     /* The following series of calls installs all our AppleEvent Handlers.
  25.     *   These handlers are added to the application event handler list that 
  26.     *   the AppleEvent manager maintains.  So, whenever an AppleEvent happens
  27.     *   and we call AEProcessEvent, the AppleEvent manager will check our
  28.     *   list of handlers and dispatch to it if there is one.
  29.     */
  30.     if (gHasAppleEvents) {
  31.  
  32.          aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, 
  33.              NewAEEventHandlerProc(AEOpenHandler),0, false);
  34.              if (aevtErr)  ExitToShell();
  35.  
  36.          aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, 
  37.              NewAEEventHandlerProc(AEOpenDocHandler),0, false);
  38.              if (aevtErr)  ExitToShell();
  39.  
  40.          aevtErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, 
  41.              NewAEEventHandlerProc(AEQuitHandler), 0, false);
  42.              if (aevtErr)  ExitToShell();
  43.  
  44.          aevtErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, 
  45.              NewAEEventHandlerProc(AEPrintHandler),0, false);
  46.              if (aevtErr)  ExitToShell();
  47.     } else {
  48.         ExitToShell();
  49.     }
  50. }
  51.  
  52. /* end InitAEStuff */
  53.  
  54. void DoHighLevel(EventRecord *AERecord)
  55. {
  56.     /* I'm not doing any error handling here because there's not a lot */
  57.     /* I can do, just pass the errors back. */
  58.     AEProcessAppleEvent(AERecord);
  59.     
  60. }
  61.  
  62. /* end DoHighLevel */
  63.  
  64.  
  65. /* This is the standard Open Application event.   */
  66. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  67. {
  68. #pragma unused (messagein,reply,refIn)
  69.     /* we of course don't do anything here, since we're background only */
  70.     return(noErr);
  71. }
  72.  
  73. /* end AEOpenHandler */
  74.  
  75. /* Open Doc, opens our documents. */
  76. /* In this case, of course, you are in the background so you should return an error */
  77. /* here since you're not opening a document. */
  78. /* Of course, you _might_ want to open a doc, but you will probably */
  79. /* confuse the user if you do, since they will see no action as the */
  80. /*  result of their clicking on a document icon. */
  81. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  82. {
  83. #pragma unused (reply, refIn,messagein)
  84.     /* we of course don't do anything here, so tell the sender that we  */
  85.     /* didn't handle the event */
  86.     return(errAEEventNotHandled);
  87. }
  88.  
  89. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  90. {
  91. #pragma unused (reply,refIn,messagein)
  92.     /* we of course don't do anything here */
  93.     return(errAEEventNotHandled);
  94. }
  95.  
  96. /* Standard Quit event handler, to handle a Quit event from the Finder, for example.  */
  97. /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life.  */
  98. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  99. {
  100. #pragma unused (messagein,refIn,reply)
  101.     /*  This does _NOT_ quit, you */
  102.     /* should NEVER quit from an AppleEvent handler.  Calling */
  103.     /* ExitToShell here would blow things up */
  104.     gQuit = true;
  105.     return(noErr);
  106. }
  107.  
  108.